package swe.article;

import java.util.ArrayList;
import java.util.List;

import swe.user.User;

public class ArticleManager {

	private String name = "ARTICLEMANAGER";
	
	private List<Article> articleList = new ArrayList<Article>();
	//private String filename = "/home/a0331084/apache-tomcat-7.0.32/webapps/buyallthestuff/WEB-INF/data/articles.ser";
	//private String filename = "/Users/matt/Dropbox/Eclipse/SWE/UE_3/buyallthestuff/WebContent/WEB-INF/data/articles.ser";
	private String filename = "/Users/simonb/Documents/PROJEKTE/Android/workspace/buyallthestuff/WebContent/WEB-INF/data/articles.ser";

	
	private ArticleDAO adao;
	
	public ArticleManager(ArticleDAO adao) {
		this.adao = adao;
		this.articleList = adao.read();

	}
	
	
	public String getName() {
		return name;
	}
	
	public List<Article> searchArticle(String string) {
		//String[] auto = {"bmw", "audi", "vw"};
		//return auto;
		
		List<Article> res = new ArrayList<Article>();
		for(int i=0; i<articleList.size(); i++) {
			if(articleList.get(i).getTitle().toLowerCase().contains(string.toLowerCase())) {
				res.add(articleList.get(i));
			}
		}
		
		return res;
	}

	public List<Article> listFromCategory(int id) {

		
		List<Article> res = new ArrayList<Article>();
		for(int i=0; i<articleList.size(); i++) {
			if(articleList.get(i).getCategory().getId() == id) {
				res.add(articleList.get(i));
			}
		}
		
		return res;
	}
	
	public Article getArticle(String id) {
		for(Article a: articleList) {
			if(a.getID().equals(id)) {
				return a;
			}
		}
		return new Article();
	}
	
	
	public void addArticle(Article a) {
		articleList.add(a);
		adao.save(articleList);
	}
	
	
	public void addBid(Article a, ArticleBid bid) {
		
		a.addBid(bid);
		
		adao.save(articleList);
	}
	
	
	public void addBuy(Article a, User u) {
		
		a.setBought(u);
		
		adao.save(articleList);
	}
	
	public List<Article> listArticles() {
		return articleList;
	}
	
	/**
	 * Returns finished articles for a user
	 * 
	 * @param user
	 * @return
	 */
	public List<Article> listFinishedArticlesForUser(User user) {
		List<Article> res = new ArrayList<Article>();

		for(Article a: articleList) {

			if(a.isActive()) {
				continue;
			}

			try {
				ArticleBid abid = a.getLastBid();

				if(abid.getUser().getUserName().equals(user.getUserName())) {
					res.add(a);
				}

			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}

		return res;		
	}
	
	/**
	 * Returns articles a user created
	 * @param user
	 * @return
	 */
	public List<Article> listCreatedArticlesForUser(User user) {
		List<Article> res = new ArrayList<Article>();

		for(Article a: articleList) {
			System.out.println(a.getUser().getUserName());
			if(a.getUser().getUserName().equals(user.getUserName())) {
				res.add(a);
			}
		}
		
		return res;		
	}
	
	public Double getAverageRating(User user){
		
		Double AvgRatingSum = 0.0;
		int AvgRatingCnt = 0;
		List<Article> ListeErstellterArtikelDesUsers = listCreatedArticlesForUser(user);
		
		for(Article a: ListeErstellterArtikelDesUsers) {
			if(a.isRated()) {
				AvgRatingCnt++;
				AvgRatingSum += (Double)a.getRating().getRating();
				
			}
		}
		
		if (AvgRatingCnt==0) {
			return 0.0;
		} else {
			return AvgRatingSum/AvgRatingCnt;
		}
	}
	
}
